package pl.task;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
    private static final String TAG = "task_MainActivity";

    private final Handler handler = new Handler();
    private final Runnable runnable1 = new Runnable()
        {
            @Override
            public void run()
            {
                final Intent intent = new Intent();
                intent.setAction("timer__timer");
                sendBroadcast(intent);
            }
        };
    private final BroadcastReceiver broadcastReceiver1 = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_LONG).show();
            }
        };
    private final BroadcastReceiver broadcastReceiver2 = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                final Intent targetIntent = new Intent(Intent.ACTION_VIEW);
                targetIntent.addCategory("pl.task.SUB_ACTIVITY");
                startActivity(targetIntent);
                finish();
            }
        };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button button = (Button) findViewById(R.id.showSubActivity1);
        button.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    final Intent targetIntent = new Intent();
                    targetIntent.setClass(MainActivity.this, SubActivity.class);
                    startActivity(targetIntent);
                }
            });
    }

    public void showSubActivity2Clicked(View v)
    {
        final Intent targetIntent = new Intent(Intent.ACTION_VIEW);
        targetIntent.addCategory("pl.task.SUB_ACTIVITY");
        startActivity(targetIntent);
    }

    public void showTasksClicked(View v)
    {
        final Intent targetIntent = new Intent(Intent.ACTION_VIEW, Task.CONTENT_URI);
        startActivity(targetIntent);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        handler.postDelayed(runnable1, 3000);
        registerReceiver(broadcastReceiver1, new IntentFilter("timer__timer"));
        registerReceiver(broadcastReceiver2, new IntentFilter("timer__timer"));
    }

    @Override
    protected void onPause()
    {
        Log.d(TAG, "onPause");

        handler.removeCallbacks(runnable1);
        unregisterReceiver(broadcastReceiver1);
        unregisterReceiver(broadcastReceiver2);
        
        super.onPause();
    }
}
